<?php 
/**
 * 
 * @author shitou
 *
 */
class OutletModel extends Model {
	
	/**
	 * 导出餐厅列表excel
	 */
	public function export($list){
		
		header("Content-type:application/octet-stream");
		header("Accept-Ranges:bytes");
		header("Content-type:application/vnd.ms-excel");
		header("Content-Disposition:attachment;filename=餐厅表格".date("Y-m-d").".xls");
		header("Pragma: no-cache");
		header("Expires: 0");
		
		$tag0 = iconv("UTF-8", "GB2312",'餐厅名称');
		$tag1 = iconv("UTF-8", "GB2312",'所属城市');
		$tag2 = iconv("UTF-8", "GB2312",'详细地址');
		$tag3 = iconv("UTF-8", "GB2312",'类型');
		$tag4 = iconv("UTF-8", "GB2312",'星级');
		$tag5 = iconv("UTF-8", "GB2312",'电话');
		$tag6 = iconv("UTF-8", "GB2312",'电邮');
		$tag7 = iconv("UTF-8", "GB2312",'税点');
		$tag8 = iconv("UTF-8", "GB2312",'添加时间');
		
		echo "$tag0\t$tag1\t$tag2\t$tag3\t$tag4\t$tag5\t$tag6\t$tag7\t$tag8\n";
		
		foreach($list as $key=>$val){
			
			$title     = iconv("UTF-8", "GB2312", $val['title']);
			$ssxadd    = iconv("UTF-8", "GB2312", $val['ssxadd']);
			$address   = iconv("UTF-8", "GB2312", $val['address']);
			$type      = iconv("UTF-8", "GB2312", $val['type']);
			$level     = iconv("UTF-8", "GB2312", $val['level']);
			$tel       = iconv("UTF-8", "GB2312", $val['tel']);
			$email     = iconv("UTF-8", "GB2312", $val['email']);
			$point     = iconv("UTF-8", "GB2312", $val['point']);
			$addtime   = iconv("UTF-8", "GB2312", date("Y-m-d H:i:s",$val['addtime']));
			
			echo "$title\t$ssxadd\t$address\t$type\t$level\t$tel\t$email\t$point\t$addtime\n";
		}
	}
	
	/**
	 * poi云检索，获取用户周边一定范围的圆形区域内的餐厅列表数据
	 */
	public function getOutletArray($query){
		
		if($query['addid']){
			$one = sendRest('RMS/Site/detail',array('id'=>$query['addid']));
			$query['pointlng'] = $one['data']['longitude'];
			$query['pointlat'] = $one['data']['latitude'];
		}
	
		$poit = $query['pointlng'].",".$query['pointlat'];
		$list = mapNearby(array("location"=>$poit));
	
		//遍历餐厅数组，将餐厅的详细数据赋给该数组
		/*foreach($list as $k=>$v){
			$map['id'] = $v['outletid'];
			$data = sendRest("BDC/Outlet/detail",$map);
				
			if($data){
				//筛选出已经停业的餐厅
				if($data['data']['is_tingye']!=1){
					$list[$k]['detail'] = $data['data'];
				}else{
					unset($list[$k]);
				}
			}
		}*/

		//遍历餐厅数组，将餐厅的详细数据赋给该数组
		foreach($list as $k=>$v){
			if($v['outletid'] == $query['restid']){
				$detail = M('Outlet')->find($query['restid']);
				$list[$k]['detail'] = $detail;break;
			}
		}
		
		return $list;
	}
	
	/**
	 * 根据餐厅信息，计算餐厅的配送时间以及服务费
	 */
	public function getTime($detail,$list,$attr){
		
		$cityarr = C('CITYID');
		
		$config = F('Sysconfig');
		$juli   = $config['sys'][$cityarr[$attr['cityid']][0]];
		//$xishu  = $config['cfg']['xishu'];
		$xishu  = 1;
		$time   = 0;
	
		foreach($juli as $m=>$n){
			foreach($list as $k=>$v){
	
				//获取是否减免服务费以及服务费
				$exempt = $this->getDelivery($detail,$n[3]);
				$isgao  = $this->getOutletGao($v['detail'],$config); //判断是否属于高峰时段
	
				if($v['outletid']==$detail['id']){
					if($n[1]){
						if($n[0]<$v['distance']&&$v['distance']<$n[1]){
							//$time = ($n[2]*2+$v['detail']['trantime'])*$xishu;
							if($v['detail']['is_yanshi']==1){
								$time = ($n[2]+$v['detail']['yanshi'])*$xishu;
							}else{
								$time = $n[2]*$xishu;
							}
	
							$time = $isgao['isgao']==1?$time+$isgao['gaotime']:$time;
							$time = $this->getIntTime($time);
							$map['exempt']   = $exempt['exempt'];
							$map['sever']    = $exempt['sever'];
							$map['distance'] = $v['distance'];
						}
					}else{
						if($n[0]<$v['distance']){
							//$time = ($n[2]*2+$v['detail']['trantime'])*$xishu;
							if($v['detail']['is_yanshi']==1){
								$time = ($n[2]+$v['detail']['yanshi'])*$xishu;
							}else{
								$time = $n[2]*$xishu;
							}
	
							$time = $isgao['isgao']==1?$time+$isgao['gaotime']:$time;
							$time = $this->getIntTime($time);
							$map['exempt']   = $exempt['exempt'];
							$map['sever']    = $exempt['sever'];
							$map['distance'] = $v['distance'];
						}
					}
					$map['time'] = $time;
				}
			}
		}
		return $map;
	}
	
	/**
	 * 判断是否开启减免运费，是否在减免运费时间段
	 */
	public function getDelivery($detail,$price){
	
		$weekarr = array("7","1","2","3","4","5","6");  //星期数组
		$week    = $weekarr[date("w")];                 //获取到今天星期几
		$exempt  = 1;                                   //免费标识  1否2是
		$start   = strtotime(date("Y-m-d").' '.$detail['exemptbegin']);               //餐厅免费开始时间
		$end     = strtotime(date("Y-m-d").' '.$detail['exemptend']);                 //餐厅免费结束时间
		$now     = strtotime(date("Y-m-d H:i"));         //当前时间
		$eprice  = $detail['exemptprice'];               //需要减免的费用
		$serve   = $price;                               //减免后的费用初始化
	
	
		//如果开启了减免配送费
		if($detail['is_exempt']==1){
			//判断是否在星期几之内
			if($detail['week']){
				$arr = explode(',',$detail['week']);
				if(in_array($week,$arr)){
					if($now>=$start&&$now<=$end){
						$exempt = 2;
						$serve  = $price-$eprice;
					}
				}
			}
		}
		return array("exempt"=>$exempt,"sever"=>$serve);
	}
	
	/**
	 * 判断是否属于高峰时段
	 * @param array $detail 餐厅详情
	 * @param array $config 系统配置
	 */
	public function getOutletGao($detail,$config){
	
		$cityid  = C('CITYID'); //系统配置
		$address = $cityid[$detail['city']];
		$timearr = $config['cfg']['gaofeng'][$address[0]];
		$now     = strtotime(date("H:i:s"));
		$isgao   = 2;
	
		if($timearr['start1']){
			$start = strtotime($timearr['start1']);
			$end   = strtotime($timearr['end1']);
				
			if($now>=$start&&$now<=$end){
				$isgao = 1;
			}
		}
	
		if($timearr['start2']){
			$start = strtotime($timearr['start2']);
			$end   = strtotime($timearr['end2']);
	
			if($now>=$start&&$now<=$end){
				$isgao = 1;
			}
		}
		return array("isgao"=>$isgao,"gaotime"=>$timearr['time']);
	}
	
	/**
	 * 根据时间，获取到整数时间
	 * @param unknown_type $time
	 */
	public function getIntTime($time){
	
		$a = substr($time,-1);
		$b = substr($time,0,-1);
	
		if($a>=0&&$a<=5){
			$a = 5;
		}
		if(5<$a&&$a<=9){
			$a = 0;
			$b+=1;
		}
	
		return $b.$a;
	}
}
?>